package br.ufrj.dcc.tp.main;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;

import br.ufrj.dcc.tp.ns.NsTracingFile;
import br.ufrj.dcc.tp.ns.NsTracingSimulationProcessor;
import br.ufrj.dcc.tp.ns.resources.ResourceLoader;
import br.ufrj.dcc.tp.ns.types.NsOutputProcessor;
import br.ufrj.dcc.tp.ns.types.NsTracingEvent;
import br.ufrj.dcc.tp.ns.types.NsTracingPacket;

public class Main {
	/**
	 * Saída do trace-all da simulação
	 */
	public static final String ARQUIVO_ENTRADA_TRACE_ALL = "{0}_trace_all.nam";
	/**
	 * Saída do trace queue do enlace que caiu
	 */
	public static final String ARQUIVO_ENTRADA_QUEUE = "{0}_trace_queue13.nam";
	/**
	 * Saída do trace queue do enlace alternativo
	 */
	public static final String ARQUIVO_ENTRADA_QUEUE_ALTERNATIVO = "{0}_trace_queue12.nam";
	public static final String ARQUIVO_SAIDA = "{0}_metrics.txt";
	public static String SUFIX = "ls";
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		BigDecimal upTime = null, streamInterval = null, linkCapacity;
		if(args.length == 4) {
			SUFIX = args[0];
			
			upTime = new BigDecimal(args[1]);
			streamInterval = new BigDecimal(args[2]);
			linkCapacity = new BigDecimal(args[3]);
		} else {
			return;
		}
		
		int dataPackets = 0, controlPackets = 0;
		File f = ResourceLoader.getResource(ARQUIVO_ENTRADA_TRACE_ALL.replace("{0}", SUFIX));
		NsTracingSimulationProcessor fileHandler = new NsTracingSimulationProcessor(new NsTracingFile(f.getAbsolutePath()));
		NsTracingEvent down, up;
		
		/**
		 * Métrica 1
		 */
		fileHandler.evaluateMetric1();
		dataPackets = fileHandler.getDataPacketsLength();
		controlPackets = fileHandler.getControlPacketsLength();
		down = fileHandler.getDownEvent();
		up = fileHandler.getUpEvent();
		fileHandler.clear();
		
		System.out.println("metrica 1:");
		System.out.println("Data: " + dataPackets);
		System.out.println("Control: " + controlPackets);
		
		/**
		 * Métrica 2
		 */
		f = ResourceLoader.getResource(ARQUIVO_ENTRADA_QUEUE.replace("{0}", SUFIX));
		fileHandler.setFile(new NsTracingFile(f.getAbsolutePath()));
		fileHandler.evaluateMetric2(down);
		NsTracingPacket firstNewRoute = fileHandler.getFirstPacketNewRoute();
		
		System.out.println("metrica 2: ");
		System.out.println(down.getTime());
		System.out.println(firstNewRoute);
		
		/**
		 * Métrica 3
		 */
		f = ResourceLoader.getResource(ARQUIVO_ENTRADA_QUEUE_ALTERNATIVO.replace("{0}", SUFIX));
		fileHandler.setFile(new NsTracingFile(f.getAbsolutePath()));
		fileHandler.evaluateMetric3(up);
		NsTracingPacket firstOldRoute = fileHandler.getFirstPacketOldRoute();
		// Pegar resultados
		System.out.println("metrica 3: ");
		System.out.println(up.getTime());
		System.out.println(firstOldRoute);
		
		NsOutputProcessor output = new NsOutputProcessor(ARQUIVO_SAIDA.replace("{0}", SUFIX));
		BigDecimal metrica2 = new BigDecimal(0.00), metrica3 = new BigDecimal(0.00);
		
		if( firstNewRoute != null ) {
			metrica2 = (firstNewRoute.getTime().subtract(down.getTime()));
		}
		
		if( firstOldRoute != null ) {
			metrica3 = (firstOldRoute.getTime().subtract(up.getTime()));
		}
		
		output.append(upTime.toPlainString());
		output.append(streamInterval.toPlainString());
		output.append(linkCapacity.toPlainString());
		output.append(Integer.toString(dataPackets));
		output.append(Integer.toString(controlPackets));
		output.append(firstNewRoute == null ? "null" : metrica2.toPlainString());
		output.append(firstOldRoute == null ? "null" : metrica3.toPlainString());
		try {
			output.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
